package com.sti.platform.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sti.platform.util.UtilDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aisino.core.common.JsonPost;
import com.aisino.core.controller.BaseController;
import com.sti.platform.model.Notice;
import com.sti.platform.service.NoticeService;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("notice")
public class NoticeController extends BaseController {
	
	@Autowired
	private NoticeService noticeService;

	/**
	 * //这里的查询条件暂时定死，以后根据需要动态 改变成 Notice notice 类型的
	 * @param request
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,Notice notice,String page,String row){
		List<Notice> list = new ArrayList<>();
		page = (page==null) ? "1" : page;
		row = (row==null) ? "10" : row;
		list = noticeService.getNoticeByPage(notice, page,row);
		request.setAttribute("noticeList", list);
		request.setAttribute("notice",notice);
		request.setAttribute("page",page);
		request.setAttribute("row",row);
		return "notice/index";
	}


	/**跳转到指定，添加修改页面*/
	@RequestMapping("jump")
	public String goNotice(String id,String optType,HttpServletRequest request){
		String viewStr = "notice/add";//跳转的地址
		if(id!=null && !"".equals(id)){//id 不为空是 编辑
			Notice notice = noticeService.getNoticeByID(id);
			request.setAttribute("notice", notice);
			request.setAttribute("optType", optType);
			viewStr = "notice/detail";
		}
		request.setAttribute("type", optType);
		return viewStr;
	}
	


	
	@RequestMapping(value="/save")
	@ResponseBody
	public Map save(final Notice notice){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){	
				noticeService.saveNotice(notice);//保存
				map.put("message", "保存成功！" );
			}
		});
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public Map update(final Notice notice){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){	
				noticeService.updateNotice(notice);//保存
				map.put("message", "修改成功！" );
			}
		});
	}
	
	
	@RequestMapping("add")
	public String add(Notice notice){
		noticeService.saveNotice(notice);//保存
		return "notice/noticeList";
	}
	

	@RequestMapping(value="/delete")
	@ResponseBody
	public Map delete(final String id){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){	
				noticeService.delNotice(id);
				map.put("message", "删除成功！");
			}
		});
	}

	@RequestMapping(value="/export")
	@ResponseBody
	public void exportExcel(Map map,Notice notice,HttpServletRequest request,HttpServletResponse response){
		//notice.setType("0");////通知 0 公告 1 所有 有的不设置
		noticeService.exportNotice(notice,response);
	}

	@RequestMapping(value="/import")
	@ResponseBody
	public void importExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response){
		noticeService.importNotice(file);
	}

}
